/**
 * Created by cxj on 2016/2/22.
 */
var grid;

var showgrid;

var bootbox_global;


//删除角色名
function deleteObj(objid)
{

    bootbox= bootbox_global;
    // alert(roleid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/workerCaselist/Delete", "obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}


//修改对象
function updateObj(objid,datarow)
{

    bootbox= bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);

    var designpicarray = obj.design_piclist.split(",");
    var construtpicarray = obj.construction_piclist.split(",");





    var alertarray = {
        form: {api: "/workerCaselist/Update", name: "alertform", id: "alertform"},
        inputs: [

            {title: "工种", type: "text", placeholder: "", verify: "empty", name: "type_name", id: "type_name",value:obj.type_name},
            {title: "姓名", type: "text", placeholder: "", verify: "empty", name: "worker_name", id: "worker_name",value:obj.worker_name},
            {title: "头像", type: "img", placeholder: "", verify: "empty", name: "worker_avatar", id: "upimgs",src:obj.worker_avatar},
            {title: "单价", type: "text", placeholder: "", verify: "empty", name: "price", id: "price",value:obj.price},
            {title: "平米", type: "text", placeholder: "", verify: "empty", name: "square", id: "square",value:obj.square},
            {title: "总价", type: "text", placeholder: "", verify: "empty", name: "amount", id: "amount",value:obj.amount},
            {title: "地址", type: "text", placeholder: "", verify: "empty", name: "address", id: "address",value:obj.address},
            {title: "状态", type: "select", id: "roles",form_name:"status",copyid:"statusselect",selectvalue:obj.status},
            {title: "开始时间", type: "date", placeholder: "", verify: "empty", name: "start_time", id: "start_time",value:format_date_with_pattern(obj.start_time,"yyyy-MM-dd")},
            {title: "结束时间", type: "date", placeholder: "", verify: "empty", name: "end_time", id: "end_time",value:format_date_with_pattern(obj.end_time,"yyyy-MM-dd")},
            {title: "发布时间", type: "date", placeholder: "", verify: "empty", name: "publish_time", id: "publish_time",value:format_date_with_pattern(obj.publish_time,"yyyy-MM-dd")},


            {title: "效果图", type: "show"},
            {title: "图1", type: "img", placeholder: "", verify: "empty", name: "design1", id: "design1",src:getArrayVal(designpicarray,0)},
            {title: "图2", type: "img", placeholder: "", verify: "", name: "design2", id: "design2",src:getArrayVal(designpicarray,1)},
            {title: "图3", type: "img", placeholder: "", verify: "", name: "design3", id: "design3",src:getArrayVal(designpicarray,2)},
            {title: "图4", type: "img", placeholder: "", verify: "", name: "design4", id: "design4",src:getArrayVal(designpicarray,3)},
            {title: "图5", type: "img", placeholder: "", verify: "", name: "design5", id: "design5",src:getArrayVal(designpicarray,4)},
            {title: "图6", type: "img", placeholder: "", verify: "", name: "design6", id: "design6",src:getArrayVal(designpicarray,5)},



            {title: "施工图", type: "show"},
            {title: "图1", type: "img", placeholder: "", verify: "empty", name: "construct1", id: "construct1",src:getArrayVal(construtpicarray,0)},
            {title: "图2", type: "img", placeholder: "", verify: "", name: "construct2", id: "construct2",src:getArrayVal(construtpicarray,1)},
            {title: "图3", type: "img", placeholder: "", verify: "", name: "construct3", id: "construct3",src:getArrayVal(construtpicarray,2)},
            {title: "图4", type: "img", placeholder: "", verify: "", name: "construct4", id: "construct4",src:getArrayVal(construtpicarray,3)},
            {title: "图5", type: "img", placeholder: "", verify: "", name: "construct5", id: "construct5",src:getArrayVal(construtpicarray,4)},
            {title: "图6", type: "img", placeholder: "", verify: "", name: "construct6", id: "construct6",src:getArrayVal(construtpicarray,5)},





        ]

    }

    bootbox.dialog({
        title: "修改信息",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}





define([
    "jquery",
    "IO",
    "bootbox",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",


], function($,IO,bootbox) {
    $(function () {




        $('#fileupload').fileupload({
            url: '/uploadWorkerCase',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {

            var imgid = $(e.target).attr('imgid');
            document.getElementById(imgid).src = laoding_gif;

        }).on('fileuploadprocessalways', function (e, data) {
            //console.log("fileuploadprocessalways",data);
        }).on('fileuploadprogressall', function (e, data) {
            //console.log("fileuploadprogressall",data);
        }).on('fileuploaddone', function (e, data) {

            console.log("upyuntype "+$('#fileupload').attr("upyuntype"));
            var imgid = $(e.target).attr('imgid');
            document.getElementById(imgid).src = data._response.result.url;
        });




        bootbox_global = bootbox;
        //每列数据列性
        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
            }},
            { title: '工种', dataIndex: 'type_name',},
            { title: '工人姓名', dataIndex: 'worker_name',},
            { title: '图片', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var  avatar = data[datarow].worker_avatar;
                return "<img class='girdimg img-thumbnail' onclick=\"viewUrl('"+avatar+"')\" src='"+avatar+"'/>"


            }},
            { title: '单价', dataIndex: 'price',},
            { title: '平方', dataIndex: 'square',},
            { title: '总价', dataIndex: 'amount',},
            { title: '地址', dataIndex: 'address',},
            { title: '周期', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var  start_time = format_date_today(data[datarow].start_time);
                var  end_time = format_date_today(data[datarow].end_time);

                return start_time+"-"+end_time;



            }},

            { title: '状态', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var  status = data[datarow].status;

                if(status == "working")
                    return "进行中";

                if(status == "finished")
                    return "已完工";




            }},

            { title: '发布时间', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                return format_date_today(data[datarow].publish_time);

            }},


            //{ title: '发布时间', dataIndex: 'publish_time',},

            { title: '效果图列表', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var designpicarray = data[datarow].design_piclist.split(",");

                var html ="";

                for(var i=0;i<designpicarray.length;i++)
                {

                    html +="<img class='girdimgthunb' onclick=\"viewUrl('"+designpicarray[i]+"')\" src='"+designpicarray[i]+"'/>&nbsp;";

                }

                return html;

            }},

            { title: '施工图列表', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var designpicarray = data[datarow].construction_piclist.split(",");

                var html ="";

                for(var i=0;i<designpicarray.length;i++)
                {

                    html +="<img class='girdimgthunb' onclick=\"viewUrl('"+designpicarray[i]+"')\" src='"+designpicarray[i]+"'/>&nbsp;";

                }

                return html;

            }},

            { title: '提交时间', dataIndex: 'create_time',},
            //{ title: '最后更新时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var html= "<div class='div_btn'><button onclick=\"updateObj("+data[datarow].case_id+","+datarow+")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj("+data[datarow].case_id+")\"  class='btn btn-danger'>删除</button>";




                html = html+"</div>"

                return html;
            }},




        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/workerCaselist/GetList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="";


            return param;

        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });


        //新增用户
        $("#btn_add").bind("click",function() {


            var alertarray = {
                form: {api: "/workerCaselist/AddCase", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "工种", type: "text", placeholder: "", verify: "empty", name: "type_name", id: "type_name"},
                    {title: "姓名", type: "text", placeholder: "", verify: "empty", name: "worker_name", id: "worker_name"},
                    {title: "头像", type: "img", placeholder: "", verify: "empty", name: "worker_avatar", id: "upimgs",src:""},
                    {title: "单价", type: "text", placeholder: "", verify: "empty", name: "price", id: "price"},
                    {title: "平米", type: "text", placeholder: "", verify: "empty", name: "square", id: "square"},
                    {title: "总价", type: "text", placeholder: "", verify: "empty", name: "amount", id: "amount"},
                    {title: "地址", type: "text", placeholder: "", verify: "empty", name: "address", id: "address"},
                    {title: "状态", type: "select", id: "roles",form_name:"status",copyid:"statusselect"},
                    {title: "开始时间", type: "date", placeholder: "", verify: "empty", name: "start_time", id: "start_time"},
                    {title: "结束时间", type: "date", placeholder: "", verify: "empty", name: "end_time", id: "end_time"},
                    {title: "发布时间", type: "date", placeholder: "", verify: "empty", name: "publish_time", id: "publish_time"},


                    {title: "效果图", type: "show"},
                    {title: "图1", type: "img", placeholder: "", verify: "empty", name: "design1", id: "design1",src:""},
                    {title: "图2", type: "img", placeholder: "", verify: "", name: "design2", id: "design2",src:""},
                    {title: "图3", type: "img", placeholder: "", verify: "", name: "design3", id: "design3",src:""},
                    {title: "图4", type: "img", placeholder: "", verify: "", name: "design4", id: "design4",src:""},
                    {title: "图5", type: "img", placeholder: "", verify: "", name: "design5", id: "design5",src:""},
                    {title: "图6", type: "img", placeholder: "", verify: "", name: "design6", id: "design6",src:""},

                    {title: "施工图", type: "show"},
                    {title: "图1", type: "img", placeholder: "", verify: "empty", name: "construct1", id: "construct1",src:""},
                    {title: "图2", type: "img", placeholder: "", verify: "", name: "construct2", id: "construct2",src:""},
                    {title: "图3", type: "img", placeholder: "", verify: "", name: "construct3", id: "construct3",src:""},
                    {title: "图4", type: "img", placeholder: "", verify: "", name: "construct4", id: "construct4",src:""},
                    {title: "图5", type: "img", placeholder: "", verify: "", name: "construct5", id: "construct5",src:""},
                    {title: "图6", type: "img", placeholder: "", verify: "", name: "construct6", id: "construct6",src:""},





                ]

            }
            bootbox.dialog({
                title: "新增信息",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });

    });






});

